package com.fms.trollalarm.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.fms.trollalarm.MainActivity;
import com.fms.trollalarm.R;
import com.fms.trollalarm.controller.AlarmController;
import com.fms.trollalarm.object.AlarmModel;

public class AlarmListAdapter extends ArrayAdapter<AlarmModel> {

	private static class ViewHolder {
		public final RelativeLayout rootView;
		public final TextView tvTime;
		public final TextView tvName;
		public final TextView tvSunday;
		public final TextView tvMonday;
		public final TextView tvTuesday;
		public final TextView tvWednesday;
		public final TextView tvThursday;
		public final TextView tvFriday;
		public final TextView tvSaturday;
		public final ToggleButton tbEnable;

		private ViewHolder(RelativeLayout rootView, TextView tvTime,
				TextView tvName, TextView tvSunday, TextView tvMonday,
				TextView tvTuesday, TextView tvWednesday, TextView tvThursday,
				TextView tvFriday, TextView tvSaturday, ToggleButton tbEnable) {
			this.rootView = rootView;
			this.tvTime = tvTime;
			this.tvName = tvName;
			this.tvSunday = tvSunday;
			this.tvMonday = tvMonday;
			this.tvTuesday = tvTuesday;
			this.tvWednesday = tvWednesday;
			this.tvThursday = tvThursday;
			this.tvFriday = tvFriday;
			this.tvSaturday = tvSaturday;
			this.tbEnable = tbEnable;
		}

		public static ViewHolder create(RelativeLayout rootView) {
			TextView tvTime = (TextView) rootView.findViewById(R.id.tv_time);
			TextView tvName = (TextView) rootView.findViewById(R.id.tv_name);
			TextView tvSunday = (TextView) rootView
					.findViewById(R.id.tv_sunday);
			TextView tvMonday = (TextView) rootView
					.findViewById(R.id.tv_monday);
			TextView tvTuesday = (TextView) rootView
					.findViewById(R.id.tv_tuesday);
			TextView tvWednesday = (TextView) rootView
					.findViewById(R.id.tv_wednesday);
			TextView tvThursday = (TextView) rootView
					.findViewById(R.id.tv_thursday);
			TextView tvFriday = (TextView) rootView
					.findViewById(R.id.tv_friday);
			TextView tvSaturday = (TextView) rootView
					.findViewById(R.id.tv_saturday);
			ToggleButton tbEnable = (ToggleButton) rootView
					.findViewById(R.id.tb_enable);
			return new ViewHolder(rootView, tvTime, tvName, tvSunday, tvMonday,
					tvTuesday, tvWednesday, tvThursday, tvFriday, tvSaturday,
					tbEnable);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.alarm_list_item, parent,
					false);
			vh = ViewHolder.create((RelativeLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final AlarmModel alarm = getItem(position);
		Log.d("isEnable",
				position + " isEnable " + String.valueOf(alarm.isEnabled()));
		vh.tvTime.setText(String.format("%02d : %02d", alarm.getTimeHour(),
				alarm.getTimeMinute()));
		vh.tvName.setText(alarm.getName());
		vh.tbEnable.setChecked(alarm.isEnabled());
		updateTextColor(vh.tvSunday, alarm.getRepeatingDay(AlarmModel.SUNDAY));
		updateTextColor(vh.tvMonday, alarm.getRepeatingDay(AlarmModel.MONDAY));
		updateTextColor(vh.tvTuesday, alarm.getRepeatingDay(AlarmModel.TUESDAY));
		updateTextColor(vh.tvWednesday,
				alarm.getRepeatingDay(AlarmModel.WEDNESDAY));
		updateTextColor(vh.tvThursday,
				alarm.getRepeatingDay(AlarmModel.THURSDAY));
		updateTextColor(vh.tvFriday, alarm.getRepeatingDay(AlarmModel.FRDIAY));
		updateTextColor(vh.tvSaturday,
				alarm.getRepeatingDay(AlarmModel.SATURDAY));

		// vh.tbEnable.setOnCheckedChangeListener(new OnCheckedChangeListener()
		// {
		//
		// @Override
		// public void onCheckedChanged(CompoundButton buttonView,
		// boolean isChecked) {
		// Log.d("sda", "click");
		// alarm.setEnabled(isChecked);
		// // ((MainActivity) mContext).setAlarmEnabled(alarm.getId(),
		// // isChecked);
		// }
		// });

		vh.tbEnable.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isChecked = setAlarmEnabled(alarm, !alarm.isEnabled());
				alarm.setEnabled(isChecked);
				vh.tbEnable.setChecked(isChecked);
			}
		});

		vh.rootView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((MainActivity) mContext).startAlarmDetailsActivity(alarm
						.getId());
			}
		});
		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private Context mContext;
	private AlarmController alarmController;

	// Constructors
	public AlarmListAdapter(Context context, List<AlarmModel> objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.alarmController = new AlarmController(context);
	}

	public AlarmListAdapter(Context context, AlarmModel[] objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
	}

	private void updateTextColor(TextView view, boolean isOn) {
		if (isOn) {
			view.setTextColor(Color.GREEN);
		} else {
			view.setTextColor(Color.BLACK);
		}
	}

	public boolean setAlarmEnabled(AlarmModel alarm, boolean isEnabled) {
		alarm.setEnabled(isEnabled);
		alarmController.updateAlarm(alarm);
		return alarmController.getAlarm(alarm.getId()).isEnabled();
	}
}